home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1457.dms / var1457.adf / Gadgets / Example4.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  13KB  |  336 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Gadgets                     Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open a normal window which is connected to the   */
  21. /* Workbench Screen. The window will use all System Gadgets, and will */
  22. /* close first when the user has selected the System gadget Close     */
  23. /* window. Inside the window we have put two Boolean gadgets with the */
  24. /* text "GADGET 1" and "GADGET 2".                                    */
  25.  
  26.  
  27.  
  28. #include <intuition/intuition.h>
  29.  
  30.  
  31.  
  32. struct IntuitionBase *IntuitionBase;
  33.  
  34.  
  35.  
  36. /* The coordinates for the box: */
  37. SHORT my_points[]=
  38. {
  39.    0,  0, /* Start at position (0,0) */
  40.   70,  0, /* Draw a line to the right to position (70,0) */
  41.   70, 10, /* Draw a line down to position (70,10) */
  42.    0, 10, /* Draw a line to the right to position (0,10) */
  43.    0,  0  /* Finish off by drawing a line up to position (0,0) */ 
  44. };
  45.  
  46. /* The Border structure: */
  47. struct Border my_border=
  48. {
  49.   0, 0,        /* LeftEdge, TopEdge. */
  50.   1,           /* FrontPen, colour register 1. */
  51.   0,           /* BackPen, for the moment unused. */
  52.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  53.   5,           /* Count, 5 pair of coordinates in the array. */
  54.   my_points,   /* XY, pointer to the array with the coordinates. */
  55.   NULL,        /* NextBorder, no other Border structures are connected. */
  56. };
  57.  
  58. /* We can use the same Border structure for both of the gadgets since */
  59. /* they have the same size. */ 
  60.  
  61.  
  62.  
  63. /************/
  64. /* GADGET 1 */
  65. /************/
  66.  
  67. /* The first text string: */
  68. UBYTE my_first_string[]="GADGET 1";
  69.  
  70. /* The IntuiText structure: */
  71. struct IntuiText my_first_text=
  72. {
  73.   1,         /* FrontPen, colour register 1. */
  74.   0,         /* BackPen, colour register 0. */
  75.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  76.              /* change the background. */ 
  77.   4, 2,      /* LeftEdge, TopEdge. */
  78.   NULL,      /* ITextFont, use default font. */
  79.   my_first_string, /* IText, the text that will be printed. */
  80.              /* (Remember my_text = &my_text[0].) */
  81.   NULL,      /* NextText, no other IntuiText structures are connected. */
  82. };
  83.  
  84. struct Gadget my_first_gadget=
  85. {
  86.   NULL,          /* NextGadget, no more gadgets in the list. */
  87.   40,            /* LeftEdge, 40 pixels out. */
  88.   20,            /* TopEdge, 20 lines down. */
  89.   71,            /* Width, 71 pixels wide. */
  90.   11,            /* Height, 11 pixels lines heigh. */
  91.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  92.                  /* will be rendered in the complement colours. */
  93.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  94.                  /* (Colour 1 (01)           - " -           2 (10) */
  95.                  /* (Colour 2 (10)           - " -           1 (01) */
  96.                  /* (Colour 3 (11)           - " -           0 (00) */  
  97.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  98.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  99.                  /* has released it. */ 
  100.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  101.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  102.                  /* (Since Intuition does not know if this will be a */
  103.                  /* pointer to a Border structure or an Image structure, */
  104.                  /* Intuition expects an APTR (normal memory pointer). */
  105.                  /* We will therefore have to calm down the compiler by */
  106.                  /* doing some "casting".) */
  107.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  108.                  /* with an alternative image. (We complement the */
  109.                  /* colours instead) */
  110.   &my_first_text,/* GadgetText, a pointer to our IntuiText structure. */
  111.                  /* (See chapter 3 GRAPHICS for more information) */
  112.   NULL,          /* MutualExclude, no mutual exclude. */
  113.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  114.                  /* (It is not a Proportional/String or Integer gdget) */
  115.   0,             /* GadgetID, no id. */
  116.   NULL           /* UserData, no user data connected to the gadget. */
  117. };
  118.  
  119.  
  120.  
  121. /************/
  122. /* GADGET 2 */
  123. /************/
  124.  
  125. /* The second text string: */
  126. UBYTE my_second_string[]="GADGET 2";
  127.  
  128. /* The IntuiText structure: */
  129. struct IntuiText my_second_text=
  130. {
  131.   1,         /* FrontPen, colour register 1. */
  132.   0,         /* BackPen, colour register 0. */
  133.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  134.              /* change the background. */ 
  135.   4, 2,      /* LeftEdge, TopEdge. */
  136.   NULL,      /* ITextFont, use default font. */
  137.   my_second_string, /* IText, the text that will be printed. */
  138.              /* (Remember my_text = &my_text[0].) */
  139.   NULL,      /* NextText, no other IntuiText structures are connected. */
  140. };
  141.  
  142. struct Gadget my_second_gadget=
  143. {
  144.   &my_first_gadget, /* NextGadget, after this comes my_first_gadget. */
  145.   150,           /* LeftEdge, 150 pixels out. */
  146.   20,            /* TopEdge, 20 lines down. */
  147.   71,            /* Width, 71 pixels wide. */
  148.   11,            /* Height, 11 pixels lines heigh. */
  149.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  150.                  /* will be rendered in the complement colours. */
  151.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  152.                  /* (Colour 1 (01)           - " -           2 (10) */
  153.                  /* (Colour 2 (10)           - " -           1 (01) */
  154.                  /* (Colour 3 (11)           - " -           0 (00) */  
  155.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  156.   RELVERIFY,     /* the user has selected this gadget, and when the user */
  157.                  /* has released it. */ 
  158.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  159.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  160.                  /* (Since Intuition does not know if this will be a */
  161.                  /* pointer to a Border structure or an Image structure, */
  162.                  /* Intuition expects an APTR (normal memory pointer). */
  163.                  /* We will therefore have to calm down the compiler by */
  164.                  /* doing some "casting".) */
  165.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  166.                  /* with an alternative image. (We complement the */
  167.                  /* colours instead) */
  168.   &my_second_text,/* GadgetText, a pointer to our IntuiText structure. */
  169.                  /* (See chapter 3 GRAPHICS for more information) */
  170.   NULL,          /* MutualExclude, no mutual exclude. */
  171.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  172.                  /* (It is not a Proportional/String or Integer gdget) */
  173.   0,             /* GadgetID, no id. */
  174.   NULL           /* UserData, no user data connected to the gadget. */
  175. };
  176.  
  177.  
  178.  
  179. /* Declare a pointer to a Window structure: */ 
  180. struct Window *my_window;
  181.  
  182. /* Declare and initialize your NewWindow structure: */
  183. struct NewWindow my_new_window=
  184. {
  185.   50,            /* LeftEdge    x position of the window. */
  186.   25,            /* TopEdge     y positio of the window. */
  187.   320,           /* Width       320 pixels wide. */
  188.   100,           /* Height      100 lines high. */
  189.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  190.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  191.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  192.                  /*             user has selected the Close window gad, */
  193.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  194.   GADGETUP,      /*             a gadge has been released. */
  195.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  196.   WINDOWCLOSE|   /*             Close Gadget. */
  197.   WINDOWDRAG|    /*             Drag gadget. */
  198.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  199.   WINDOWSIZING|  /*             Sizing Gadget. */
  200.   ACTIVATE,      /*             The window should be Active when opened. */
  201.   &my_second_gadget, /* FirstGadget A pointer to my_second_gadget
  202.                      /* structure. */
  203.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  204.   "TOUCH ME",    /* Title       Title of the window. */
  205.   NULL,          /* Screen      Connected to the Workbench Screen. */
  206.   NULL,          /* BitMap      No Custom BitMap. */
  207.   320,           /* MinWidth    We will not allow the window to become */
  208.   50,            /* MinHeight   smaller than 320 x 50, and not bigger */
  209.   640,           /* MaxWidth    than 640 x 200. */
  210.   200,           /* MaxHeight */
  211.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  212. };
  213.  
  214.  
  215.  
  216. main()
  217. {
  218.   /* Boolean variable used for the while loop: */
  219.   BOOL close_me;
  220.  
  221.  
  222.   /* Declare a variable in which we will store the IDCMP flag: */
  223.   ULONG class;
  224.  
  225.   /* In this example we also need to store the address of the gadget */
  226.   /* which sent us the mesage: */
  227.   APTR address;
  228.   
  229.   /* we declare a memory pointer (APTR) called address. */
  230.  
  231.    
  232.   /* Declare a pointer to an IntuiMessage structure: */
  233.   struct IntuiMessage *my_message;
  234.  
  235.  
  236.  
  237.   /* Before we can use Intuition we need to open the Intuition Library: */
  238.   IntuitionBase = (struct IntuitionBase *)
  239.     OpenLibrary( "intuition.library", 0 );
  240.   
  241.   if( IntuitionBase == NULL )
  242.     exit(); /* Could NOT open the Intuition Library! */
  243.  
  244.  
  245.  
  246.   /* We will now try to open the window: */
  247.   my_window = (struct Window *) OpenWindow( &my_new_window );
  248.   
  249.   /* Have we opened the window succesfully? */
  250.   if(my_window == NULL)
  251.   {
  252.     /* Could NOT open the Window! */
  253.     
  254.     /* Close the Intuition Library since we have opened it: */
  255.     CloseLibrary( IntuitionBase );
  256.  
  257.     exit();  
  258.   }
  259.  
  260.  
  261.  
  262.   /* We have opened the window, and everything seems to be OK. */
  263.  
  264.  
  265.  
  266.   close_me = FALSE;
  267.  
  268.   /* Stay in the while loop until the user has selected the Close window */
  269.   /* gadget: */
  270.   while( close_me == FALSE )
  271.   {
  272.     /* Wait until we have recieved a message: */
  273.     Wait( 1 << my_window->UserPort->mp_SigBit );
  274.  
  275.     /* Collect the message: */
  276.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  277.  
  278.     /* Have we collected the message sucessfully? */
  279.     if(my_message)
  280.     {
  281.       /* After we have collected the message we can read it, and save any */
  282.       /* important values which we maybe want to check later: */
  283.       class = my_message->Class;      /* Save the IDCMP flag. */
  284.       address = my_message->IAddress; /* Save the address. */
  285.   
  286.       /* After we have read it we reply as fast as possible: */
  287.       /* REMEMBER! Never try to read a message after you have replied! */
  288.       /* Some other process has maybe changed it. */
  289.       ReplyMsg( my_message );
  290.   
  291.       /* Check which IDCMP flag was sent: */
  292.       switch( class )
  293.       {
  294.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  295.                close_me=TRUE;
  296.                break;
  297.                
  298.         case GADGETDOWN:   /* The user has pressed on one of the Boolean */
  299.                            /* gadgets. We have now to check which: */
  300.                if( address == (APTR) &my_first_gadget)
  301.                  printf("Gadget 1 Down\n");
  302.                else
  303.                  printf("Gadget 2 Down\n");
  304.   
  305.                /* We need to do some "casting" here again since APTR is a */
  306.                /* normal memory pointer, while &my_first_gadget is a */
  307.                /* pointer to a Gadget structure. It is actually the same */
  308.                /* thing but we need to explain this for the compiler. */
  309.   
  310.                break;
  311.                
  312.         case GADGETUP:     /* The user has released one of the Boolean */
  313.                            /* gadgets. We have now to check which: */
  314.                if( address == (APTR) &my_first_gadget)
  315.                  printf("Gadget 1 Up\n");
  316.                else
  317.                  printf("Gadget 2 Up\n");
  318.                break;
  319.       }
  320.     }
  321.   }
  322.  
  323.  
  324.  
  325.   /* We should always close the windows we have opened before we leave: */
  326.   CloseWindow( my_window );
  327.  
  328.  
  329.  
  330.   /* Close the Intuition Library since we have opened it: */
  331.   CloseLibrary( IntuitionBase );
  332.   
  333.   /* THE END */
  334. }
  335.  
  336.